package com.vincce.fitness.core.config;

import at.pollux.thymeleaf.shiro.dialect.ShiroDialect;
import com.vincce.fitness.core.base.FastJsonJsonView;
import nz.net.ultraq.thymeleaf.LayoutDialect;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.oxm.xstream.XStreamMarshaller;
import org.springframework.web.accept.ContentNegotiationManager;
import org.springframework.web.accept.ContentNegotiationManagerFactoryBean;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;
import org.springframework.web.servlet.View;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.view.ContentNegotiatingViewResolver;
import org.springframework.web.servlet.view.xml.MarshallingView;
import org.thymeleaf.dialect.IDialect;
import org.thymeleaf.spring4.SpringTemplateEngine;
import org.thymeleaf.spring4.dialect.SpringStandardDialect;
import org.thymeleaf.spring4.view.ThymeleafViewResolver;
import org.thymeleaf.templateresolver.ServletContextTemplateResolver;
import org.thymeleaf.templateresolver.TemplateResolver;

import java.nio.charset.Charset;
import java.util.*;

/**
 * Created by henry on 4/17/14.
 */
@Configuration
public class WebConfig extends WebMvcConfigurerAdapter {

	/*@Bean
	public UserSessionInterceptor userSessionInterceptor() {
		return new UserSessionInterceptor();
	}*/


	@Override
	public void addInterceptors(InterceptorRegistry registry) {
		super.addInterceptors(registry);
		//registry.addInterceptor(userSessionInterceptor()).addPathPatterns("/wap/**");
	}

	@Bean
	public CommonsMultipartResolver multipartResolver() {
		CommonsMultipartResolver commonsMultipartResolver = new CommonsMultipartResolver();
		commonsMultipartResolver.setMaxUploadSize(3000000);
		return commonsMultipartResolver;
	}

	@Bean
	public RestTemplate restTemplate() {
		RestTemplate restTemplate = new RestTemplate();
		List<HttpMessageConverter<?>> converters = new ArrayList<HttpMessageConverter<?>>();
		converters.add(stringHttpMessageConverter());
		restTemplate.setMessageConverters(converters);
		return restTemplate;
	}

	public StringHttpMessageConverter stringHttpMessageConverter(){
		return new StringHttpMessageConverter(Charset.forName("UTF-8"));
	}

	@Override
	public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
		converters.add(stringHttpMessageConverter());
		super.configureMessageConverters(converters);
	}

	@Bean
	public TemplateResolver templateResolver() {
		ServletContextTemplateResolver resolver = new ServletContextTemplateResolver();
		resolver.setPrefix("/WEB-INF/views/");
		resolver.setSuffix(".html");
		resolver.setTemplateMode("HTML5");
		resolver.setCharacterEncoding("UTF-8");
		resolver.setCacheable(false);
		return resolver;
	}

	@Bean
	public SpringTemplateEngine templateEngine() {
		Set<IDialect> additionalDialects = new HashSet<IDialect>();
		additionalDialects.add(new SpringStandardDialect());
		additionalDialects.add(new ShiroDialect());
        additionalDialects.add(new LayoutDialect());
		SpringTemplateEngine engine = new SpringTemplateEngine();
		engine.setTemplateResolver(templateResolver());
		engine.setAdditionalDialects(additionalDialects);
		return engine;
	}

	@Bean
	public ContentNegotiationManager contentNegotiationManager() {
		ContentNegotiationManagerFactoryBean contentNegotiationManager = new ContentNegotiationManagerFactoryBean();
		Properties mediaTypes = new Properties();
		mediaTypes.put("json", MediaType.APPLICATION_JSON_VALUE);
		mediaTypes.put("api", MediaType.TEXT_PLAIN_VALUE);
		mediaTypes.put("htm", MediaType.TEXT_HTML_VALUE);
		mediaTypes.put("xml", MediaType.APPLICATION_XML_VALUE);
		contentNegotiationManager.setMediaTypes(mediaTypes);
		contentNegotiationManager.setDefaultContentType(MediaType.TEXT_HTML);
		contentNegotiationManager.setIgnoreAcceptHeader(true);
		return contentNegotiationManager.getObject();
	}

	@Bean
	public ViewResolver contentNegotiatingViewResolver(
			ContentNegotiationManager contentNegotiationManager,
			SpringTemplateEngine templateEngine
	) {
		ContentNegotiatingViewResolver viewResolver = new ContentNegotiatingViewResolver();
		viewResolver.setOrder(1);
		viewResolver.setContentNegotiationManager(contentNegotiationManager);

		List<ViewResolver> viewResolvers = new ArrayList<ViewResolver>();
		ThymeleafViewResolver thymeleafViewResolver = new ThymeleafViewResolver();
		thymeleafViewResolver.setTemplateEngine(templateEngine);
		thymeleafViewResolver.setCharacterEncoding("UTF-8");
		viewResolvers.add(thymeleafViewResolver);
		viewResolver.setViewResolvers(viewResolvers);

		List<View> defaultViews = new ArrayList<View>();
		defaultViews.add(new FastJsonJsonView());
		XStreamMarshaller xStreamMarshaller = new XStreamMarshaller();
		xStreamMarshaller.setAutodetectAnnotations(true);
		MarshallingView marshallingView = new MarshallingView(xStreamMarshaller);
		defaultViews.add(marshallingView);
		viewResolver.setDefaultViews(defaultViews);

		return viewResolver;
	}


}
